﻿namespace NewsSystem.WebApi.DependenciesResolvers
{
    using NewsSystem.Contracts;
    using NewsSystem.Data;
    using NewsSystem.Repositories;
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Web.Http.Dependencies;

    public class DbDependencyResolver : IDependencyResolver
    {
        private static readonly DbContext context = new NewsContext();

        private static readonly INewsUnitOfWork unitOfWork = new NewsSystemUnitOfWork(context);

        public IDependencyScope BeginScope()
        {
            return this;
        }

        public object GetService(Type serviceType)
        {
            try
            {
                return Activator.CreateInstance(serviceType, unitOfWork);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return new List<object>();
        }

        public void Dispose()
        {
        }
    }
}